#include <vector>
#include <string>
#include <iostream>
#include <deque>
#include "conio.h"

using namespace std;

void find_maximums_between_pointers(const vector<int> &v, const vector<char> &m)
{
	int l = 0, r = 0;
	deque<int> max_candidates;
	max_candidates.push_front(v[l]);			

	for (int i_move = 0; i_move < static_cast<int>(m.size()); ++i_move)
	{
		if (m[i_move] == 'L' || m[i_move] == 'l')
		{
			++l;
			if (v[l-1] == max_candidates.back())
			{
				max_candidates.pop_back();
			}			
		}
		else if (m[i_move] == 'R' || m[i_move] == 'r')
		{
			++r;
			// new maximum candidate
			if (v[r] > max_candidates.front())
			{
				while (!max_candidates.empty() && v[r] > max_candidates.front())
				{
					max_candidates.pop_front();
				}				
			}		
			max_candidates.push_front(v[r]);			
		}
		else
			throw string("WTF?");

		cout << max_candidates.back() << " ";
	}
}

void main()
{
	int n_elements, n_moves;
	vector<int> v;
	vector<char> m;
// 	cout << "input number of elements:" << endl;
// 	cin >> n_elements;	
// 	cout << "input elements:" << endl;
// 	v.resize(n_elements);
// 	for (int i_elem = 0; i_elem < n_elements; ++i_elem)
// 	{
// 		cin >> v[i_elem];
// 	}

	//n_elements= 11;
	int arr_v[] = {1, 2, 3, 1, 2, 3, 1, 2, 3, 8, 1};
	v = vector<int>(arr_v, arr_v + sizeof(arr_v) / sizeof(int));

// 	cout << "input number of moves:" << endl;
// 	cin >> n_moves;
// 	cout << "input moves:" << endl;
// 	m.resize(n_moves);
// 	for (int i_move = 0; i_move < n_moves; ++i_move)
// 	{
// 		cin >> m[i_move];
// 	}
	
	char arr_m[] = {'R', 'R', 'L', 'L', 'R', 'R', 'R', 'R', 'R', 'R', 'L', 'L', 'L', 'L', 'L', 'L', 'R', 'R', 'L', 'L'};
	m = vector<char>(arr_m, arr_m + sizeof(arr_m) / sizeof(char));

	find_maximums_between_pointers(v, m);
	_getch();
}